
const ADSdk = require("ADSdk");
var UIManage = cc.Class({

    extends: cc.Component,
    statics:
    {
        Instance: null,
    },

    onLoad() {
        UIManage.Instance = this;
        this.NeedStartEnvent = false;
        this.ChildrenRankCom = cc.find("wx").getComponent("ChildrenRank");

        ADSdk.init();
        cc.log("ADSdk.init")
    },

    properties: {
        UIPop: cc.Node,
        UIMian: cc.Node,
        UIApp: cc.Node,
        UIList: [],
        Starting: cc.Node,
        /*
                Gameing:cc.Prefab,
                UIReadCache:cc.Prefab,
                UINextLevel:cc.Prefab,
                UINextLevelWonderful:cc.Prefab,
                UIGameOver:cc.Prefab,
                UIDiamon:cc.Prefab,
                Starting:cc.Prefab,
                UIGetBox:cc.Prefab,
                UIGetSkill:cc.Prefab,
                UIRanking:cc.Prefab,
                UICache:cc.Prefab,
                UIResurt:cc.Prefab,
                UIRedMoney:cc.Prefab,
                UIGiftBag:cc.Prefab,
                UIBtnAppList:cc.Prefab,
                UITimeGift:cc.Prefab,
                UIOtherGame:cc.Prefab,
                */
        SceneState: "",
    },
    /*
    loderPrefabs(prefab,uiparent)
    {
        if(this.UIList[prefab.name] == undefined||this.UIList[prefab.name] == null)
        {
            var UINode = cc.instantiate(prefab);
            uiparent.addChild(UINode);
            this.UIList[prefab.name] = UINode;
        }
        else
        {
            var UINode = this.UIList[prefab.name];
        }
        return UINode;
    },
    */
    loderPrefabs(uiname, parentNode) {
        cc.log(uiname)
        return new Promise((resolve, reject) => {
            var UINode = null;
            if (this.UIList[uiname] == undefined || this.UIList[uiname] == null) {
                var path = "Prefabs/UIPrefab/Pop/" + uiname;
                cc.loader.loadRes(path, cc.Prefab, (err, prefab) => {
                    UINode = cc.instantiate(prefab);
                    UINode.parent = parentNode;
                    this.UIList[uiname] = UINode;
                    resolve(UINode)
                });
            }
            else {
                UINode = this.UIList[uiname];
                resolve(UINode)
            }
        })
    },

    ShowGameing(action) {
        if (this.SceneState == "Gaming")
            return;
        this.SceneState = "Gaming";
        var self = this;
        this.loderPrefabs("Gaming", this.UIMian).then((Node) => {
            self.Gameing = Node;
            self.Gameing.active = true;
            self.Starting.active = false;
            action();
        });

    },

    ShowGameStart() {
        var self = this;
        self.Starting.active = true;
        if (self.Gameing) {
            self.Gameing.active = false;
        }
        self.SceneState = "Start";
        self.ChildrenRankCom.HideChild();
        self.ChildrenRankCom.ResetChildMaxScore();
        if (self.NeedStartEnvent) {
            this.Starting.getComponent("UIStart").ShowSeverInfo();
            self.NeedStartEnvent = false;
        }

        if(!this.hadBanner){
            this.hadBanner = true;
            this.Starting.getComponent("UIStart").showBanner();
        }

    },

    OpenStartUI() {
        // if (this.Starting) {
        //     this.Starting.getComponent("UIStart").ShowSeverInfo();
        // }
        // else {
        //     this.NeedStartEnvent = true;
        // }
    },

    ShowUIReadCache() {
        this.loderPrefabs("UIReadCache", this.UIPop).then((UINode) => {
            UINode.getComponent("BasePopUI").Show();
        });

    },

    ShowNextUI() {
        this.loderPrefabs("UINextLevel", this.UIPop).then((UINode) => {
            UINode.getComponent("BasePopUI").Show();
        });
    },

    ShowNextUIWonderful() {
        this.loderPrefabs("UIPropNextLevel", this.UIPop).then((UINode) => {
            UINode.getComponent("BasePopUI").Show();
        });
    },

    ShowGameoverUI() {
        this.loderPrefabs("UIGameOver", this.UIPop).then((UINode) => {
            UINode.getComponent("BasePopUI").Show();
        });
    },

    ShowUIDiamon(action = null) {
        this.loderPrefabs("UIDiamon", this.UIPop).then((UINode) => {
            UINode.getComponent("BasePopUI").Show(action);
            UINode.setSiblingIndex(UINode.parent.children.length - 1);
            UINode.getComponent("BasePopUI").action = action;
        });
    },

    ShowGetBoxUI() {

        if (Utils.isPopRed()) {
            // this.ShowOpenMoney((src)=>{
            //     src.init(3);
            // });
        } else {
            this.loderPrefabs("UIGetBox", this.UIPop).then((UINode) => {
                UINode.getComponent("BasePopUI").Show();
            });
        }

    },

    ShowGetMoney() {
        // this.loderPrefabs("UIRedBag",this.UIPop).then((UINode)=>
        // {
        //     UINode.setSiblingIndex(UINode.parent.children.length-1);
        //     UINode.getComponent("UIRedBag").Show();
        // });

    },

    ShowOpenMoney(cb) {
        console.log("显示红包")
        // this.loderPrefabs("RedPacket", this.UIPop).then((UINode) => {
        //     let src = UINode.getComponent("RedPacket");
        //     src.Show();
        //     cb && cb(src);
        // });
    },

    ShowCashOut(cb) {
        this.loderPrefabs("CashOut", this.UIPop).then((UINode) => {
            let src = UINode.getComponent("CashOut");
            src.Show();
            cb && cb(src);
        });
    },

    ShowSmallPacket(cb) {
        this.loderPrefabs("SmallPacket", this.UIPop).then((UINode) => {
            let src = UINode.getComponent("SmallPacket");
            src.Show();
            cb && cb(src);
        });
    },

    ShowRedPacketActive(cb) {
        this.loderPrefabs("RedPacketActive", this.UIPop).then((UINode) => {
            let src = UINode.getComponent("RedPacketActive");
            src.Show();
            cb && cb(src);
        });
    },

    ShowToast(str) {
        let toastParent = cc.find("Toast");
        for (let obj of toastParent.children) {
            obj.getComponent('Toast').moveUp();
        }

        let path = "Prefabs/UIPrefab/Pop/Toast";
        cc.loader.loadRes(path, cc.Prefab, (err, prefab) => {
            let UINode = cc.instantiate(prefab);
            UINode.parent = toastParent;
            UINode.getComponent('Toast').show(str);
        });
    },

    ShowGetSkill() {
        this.loderPrefabs("UIGetSkill", this.UIPop).then((UINode) => {
            UINode.getComponent("BasePopUI").Show();
        });
    },

    ShowUIRanking() {
        // this.loderPrefabs("UIRanking1",this.UIPop).then((UINode)=>
        // {
        //     UINode.getComponent("UIRanking").Open();
        // });
    },

    ShowUICache() {
        this.loderPrefabs("UICache", this.UIPop).then((UINode) => {
            UINode.getComponent("UICache").Show();
        });
    },

    ShowUIResurt() {
        this.loderPrefabs("UIResurtUI", this.UIPop).then((UINode) => {
            UINode.getComponent("UIResurt").Show();
        });
    },

    ShowGiftBag() {
        this.loderPrefabs("UIGiftBag", this.UIPop).then((UINode) => {
            UINode.getComponent("UIGiftBag").Show();
        });
    },


    ShowUIHintProp(action) {
        this.loderPrefabs("UIHintProp", this.UIPop).then((UINode) => {
            UINode.getComponent("UIHintProp").Show(action);
        });
    },

    ShowAppBtn(AppIDInfoList, Applength) {
        // this.loderPrefabs("BtnAppList",this.UIApp).then((UINode)=>
        // {
        //     UINode.getComponent("UIBtnApp").ShowItem(AppIDInfoList,Applength)
        // });
    },

    ShowUITimeGift() {
        this.loderPrefabs("UITimeGift", this.UIPop).then((UINode) => {
            UINode.getComponent("UITimeGift").Show();
        });
    },

    ShowOtherGamePanel() {
        // this.loderPrefabs("UIOtherGame",this.UIPop).then((UINode)=>
        // {
        //     UINode.getComponent("UIOtherGame").Show();
        // });
    },

    ShowUIShareFriend() {
        // this.loderPrefabs("UIShareFriend",this.UIPop).then((UINode)=>
        // {
        //     UINode.getComponent("UIShareFriend").Show();
        // });
    },

});

